using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace BitMango
{
	public class AotSafe
	{
		public static void ForEach<T>(object enumerable, Action<T> action)
		{
			if (enumerable == null)
			{
				return;
			}
			Type type = enumerable.GetType().GetInterfaces().First((Type x) => !x.IsGenericType && x == typeof(IEnumerable));
			if (type == null)
			{
				throw new ArgumentException("Object does not implement IEnumerable interface", "enumerable");
			}
			MethodInfo method = type.GetMethod("GetEnumerator");
			if (method == null)
			{
				throw new InvalidOperationException("Failed to get 'GetEnumberator()' method info from IEnumerable type");
			}
			IEnumerator enumerator = null;
			try
			{
				enumerator = (IEnumerator)method.Invoke(enumerable, null);
				if (enumerator != null)
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						action((T)((object)obj));
					}
				}
				else
				{
					UnityEngine.Debug.Log(string.Format("{0}.GetEnumerator() returned '{1}' instead of IEnumerator.", enumerable.ToString(), enumerator.GetType().Name));
				}
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
		}
	}
}
